import Vue from 'vue'
import VueRouter from 'vue-router'
// const Login = () => import('@/views/login')
import store from '@/store'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: () => import('@/views/login') },
    {
      path: '/',
      redirect: '/dashboard',
      component: () => import('@/views/layout'),
      children: [
        { path: '/dashboard', component: () => import('@/views/dashboard') },
        { path: '/article', component: () => import('@/views/article') }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 获取 token
  const token = store.state.user.token
  // console.log('这是 token:', token)
  // 判断有没有 token
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      // 因为没有登录, 但访问了需要登录的页面
      next('/login')
      // 提示用户
      Message.error('请您先登录哦小笨蛋')
    }
  }
})

export default router
